package logic

import (
	"core/common"
	"core/component/logger"
	"encoding/json"
	"fmt"
	"net/http"
	"runtime"
)

func sendHttpResponse(code int, desc string, data interface{}, w http.ResponseWriter) {
	w.Header().Add("Content-Type", "application/json; charset=utf-8")
	dataBytes, _ := json.Marshal(data)
	gcResp := &common.HttpResponse{
		Code: code,
		Desc: desc,
		Data: dataBytes,
	}
	if val, ok := data.(*common.GetARandServerResp); ok {
		gcResp.StrData = val.SInfo.Addr
	}

	b, err := json.Marshal(gcResp)
	if err != nil {
		logger.Errorf("json Marshal error %v", err)
	}
	w.Write(b)
}

func addGatewayServer(w http.ResponseWriter, r *http.Request) {
	req := &common.AddServerReq{}
	err := json.NewDecoder(r.Body).Decode(req)
	if err != nil {
		logger.Infof("[GM] json.NewDecoder error: %v", err)
		return
	}
	centerServerInstance().addGatewayServer(req.SI.ServerID, req.SI.Addr)
	sendHttpResponse(0, "ok", nil, w)
}

func removeGatewayServer(w http.ResponseWriter, r *http.Request) {
	req := &common.RemoveServerReq{}
	err := json.NewDecoder(r.Body).Decode(req)
	if err != nil {
		logger.Infof("[GM] json.NewDecoder error: %v", err)
		return
	}
	centerServerInstance().removeGatewayServer(req.ServerID)
	sendHttpResponse(0, "ok", nil, w)
}

func getAllGatewayServer(w http.ResponseWriter, r *http.Request) {
	resp := &common.GetAllServerInfoResp{}
	resp.SInfos = centerServerInstance().getAllGatewayServerInfo()
	sendHttpResponse(0, "ok", resp, w)
}

func addPvpServer(w http.ResponseWriter, r *http.Request) {
	req := &common.AddServerReq{}
	err := json.NewDecoder(r.Body).Decode(req)
	if err != nil {
		logger.Infof("[GM] json.NewDecoder error: %v", err)
		return
	}
	centerServerInstance().addPvpServer(req.SI.ServerID, req.SI.Addr)
	sendHttpResponse(0, "ok", nil, w)
}

func removePvpServer(w http.ResponseWriter, r *http.Request) {
	req := &common.RemoveServerReq{}
	err := json.NewDecoder(r.Body).Decode(req)
	if err != nil {
		logger.Infof("[GM] json.NewDecoder error: %v", err)
		return
	}
	centerServerInstance().removePvpServer(req.ServerID)
	sendHttpResponse(0, "ok", nil, w)
}

func getAllPvpServer(w http.ResponseWriter, r *http.Request) {
	resp := &common.GetAllServerInfoResp{}
	resp.SInfos = centerServerInstance().getAllPvpServerInfo()
	sendHttpResponse(0, "ok", resp, w)
}

func addTcpServer(w http.ResponseWriter, r *http.Request) {
	req := &common.AddServerReq{}
	err := json.NewDecoder(r.Body).Decode(req)
	if err != nil {
		logger.Infof("[GM] json.NewDecoder error: %v", err)
		return
	}
	centerServerInstance().addTcpServer(req.SI.ServerID, req.SI.Addr, req.SI.Port)
	sendHttpResponse(0, "ok", nil, w)
}

func removeTcpServer(w http.ResponseWriter, r *http.Request) {
	req := &common.RemoveServerReq{}
	err := json.NewDecoder(r.Body).Decode(req)
	if err != nil {
		logger.Infof("[GM] json.NewDecoder error: %v", err)
		return
	}
	centerServerInstance().removeTcpServer(req.ServerID)
	sendHttpResponse(0, "ok", nil, w)
}

func getAllTcpServer(w http.ResponseWriter, r *http.Request) {
	resp := &common.GetAllServerInfoResp{}
	resp.SInfos = centerServerInstance().getAllTcpServerInfo()
	sendHttpResponse(0, "ok", resp, w)
}

func registerTopic(w http.ResponseWriter, r *http.Request) {
	req := &common.ReqRegisterTopicToCenter{}
	err := json.NewDecoder(r.Body).Decode(req)
	if err != nil {
		logger.Infof("[GM] json.NewDecoder error: %v", err)
		return
	}
	if val, ok := onlineClientMgrGetMe().topicChanelClient.Load(req.Topic); ok {
		if _, exist := val.(map[string]int)[req.ServerID]; exist {
			return
		}
	}
	onlineClientMgrGetMe().registerTopic <- req
}

func getCenterServer(w http.ResponseWriter, r *http.Request) {
	resp := &common.GetAllServerInfoResp{}
	if ServerInstance().id != "" {
		resp.SInfos = append(resp.SInfos, &common.ServerInfo{
			ServerID: ServerInstance().id,
			Addr:     ServerInstance().tcpSrvAddr,
		})
	}
	sendHttpResponse(0, "ok", resp, w)
}

func addOnlineHttPort(w http.ResponseWriter, r *http.Request) {
	req := &common.AddServerReq{}
	err := json.NewDecoder(r.Body).Decode(req)
	if err != nil {
		logger.Infof("[GM] json.NewDecoder error: %v", err)
		return
	}
	centerServerInstance().addOnlinePort(req.SI.ServerID, req.SI.Port)
	sendHttpResponse(0, "ok", nil, w)
}

func removeOnlineHttpPort(w http.ResponseWriter, r *http.Request) {
	req := &common.RemoveServerReq{}
	err := json.NewDecoder(r.Body).Decode(req)
	if err != nil {
		logger.Infof("[GM] json.NewDecoder error: %v", err)
		return
	}
	centerServerInstance().removeOnlinePort(req.ServerID, req.Port)
	sendHttpResponse(0, "ok", nil, w)
}

func getARandServerInfo(w http.ResponseWriter, r *http.Request) {
	resp := &common.GetARandServerResp{}
	resp.SInfo = centerServerInstance().getARandServerInfo()
	ip := "127.0.0.1"
	if runtime.GOOS == "linux" {
		ip = "47.117.5.200"
	}
	resp.SInfo.Addr = fmt.Sprintf("%s:%d", ip, resp.SInfo.Port)
	sendHttpResponse(0, "ok", resp, w)
}
